package com.jr.controller;

import com.jr.entry.BookInfo;
import com.jr.entry.Users;
import com.jr.mapper.UsersMapper;
import com.jr.pojo.BookInfoSelect;
import com.jr.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;


import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UsersController {
    private Integer i=9;
    @Autowired
    private UsersService usersService;

   /* @RequestMapping("index")
    public String  index(){
        return "index";
    }*/

    @RequestMapping("login")
    @ResponseBody
    public Users login(String user_code, String password, HttpSession session){
        System.out.println(user_code);
        Users users=new Users();
        users.setUser_code(user_code);
        users.setPassword(password);
        Users loginUsers=usersService.login(users);
       return loginUsers;
    }


   /*全部查询*/
    @RequestMapping("getAll")
    @ResponseBody
    public List<BookInfo> getAll(){
        return usersService.findAll();
    }
  /*条件查询*/
    @RequestMapping("getBy")
    @ResponseBody
    public List<BookInfo> getBy(BookInfoSelect bookInfo) {
        System.out.println(usersService.findByBy(bookInfo)+"=======================");
        return usersService.findByBy(bookInfo);
    }
/*添加*/
    @RequestMapping("insertBookInfo")
    @ResponseBody
    public String insertBookInfo(BookInfo bookInfo){
        boolean b=usersService.addBook(bookInfo);
        if (b){
            return "true";
        }else {
            return "false";
        }

    }
    /*修改*/
    @RequestMapping("updateBookInfo")
    @ResponseBody
    public boolean updateBookInfo(BookInfo bookInfo){
        System.out.println("Received bookInfo: " + bookInfo);
        boolean b=usersService.updBook(bookInfo);
        /*if (b){
            return true;
        }else {
            return false;
        }*/
        return b;
    }

    /*删除*/
    @RequestMapping("deleteBookInfo")
    @ResponseBody
    public boolean deleteBookInfo(Integer book_id){
        boolean b=usersService.delBook(book_id);
        if (b){
            return true;
        }else {
            return false;
        }
    }
    @RequestMapping("getFenYe")
    @ResponseBody
    public Map<String, Object> getFenYe(@RequestBody Map<String, Integer> params) {
        Integer page = params.get("page");
        Integer pageSize = params.get("pageSize");

        System.out.println("Received page: " + page);
        System.out.println("Received pageSize: " + pageSize);

        if (page == null || pageSize == null) {
            page = 1;  // 默认值
            pageSize = 10;  // 默认值
        }

        List<BookInfo> books = usersService.findByPage(page, pageSize);
        Integer total = usersService.findAll().size();
        Map<String, Object> result = new HashMap<>();
        result.put("books", books);
        result.put("total", total);
        return result;
    }
}
